package org.sardine.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {
	
	public static final String VALUE_SEPARATOR = ",";
	public static final String DESCRIPTION_SEPARATOR = "(\\s)+";
	public static final String WILDCARD = "\\?|\\*";
	public static final String PATH_AND_WILDCARD = "(\\w|\\.|/|"+WILDCARD+")+";
	public static final String VALUE_AND_WILDCARD = "(\\w|,|"+WILDCARD+")+";
	public static final String SINGLE_AUTHORIZATION_DESCRIPTION = PATH_AND_WILDCARD+"="+VALUE_AND_WILDCARD;
	public static final String AUTHORIZATION_DESCRIPTION = "("+SINGLE_AUTHORIZATION_DESCRIPTION +"("+ DESCRIPTION_SEPARATOR+"))*"+SINGLE_AUTHORIZATION_DESCRIPTION;
	public static final String XML_FILE_NAME = "(\\w+\\/)*\\w+\\.xml";			//xxx.xml或xxx/xxx/xxx.xml

	
	public static boolean matchDescriptionSeparator(String separator){
		return match(separator,DESCRIPTION_SEPARATOR);
	}
	
	public static boolean matchPathAndWildcard(String path){
		return match(path,PATH_AND_WILDCARD);
	}
	
	public static boolean matchValueAndWildcard(String value){
		return match(value,VALUE_AND_WILDCARD);
	}
	
	public static boolean matchSingleAuthorizationDescription(String singleAuthorizationDescription){
		return match(singleAuthorizationDescription,SINGLE_AUTHORIZATION_DESCRIPTION);
	}
	
	public static boolean matchAuthorizationDescription(String authorizationDescription){
		return match(authorizationDescription,AUTHORIZATION_DESCRIPTION);
	}
	
	

	
	public static boolean matchXmlFileName(String xmlFileName){
		return match(xmlFileName,XML_FILE_NAME);
	}
	
	
	
	
	
	private static boolean match(String str,String pattern){
		Pattern p = Pattern.compile(pattern);
		Matcher matcher = p.matcher(str);
		return matcher.matches();
	}

}
